/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.techshop.dao.impl;

import com.techshop.dao.CategoryDao;
import com.techshop.model.Category;
import com.techshop.util.MyUtil;
import java.util.List;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import redis.client.ValidatePhonePubSubClient;

/**
 *
 * @author Hai-PC
 */
public class CategoryDaoImpl extends HibernateDaoSupport implements CategoryDao {

    @Override
    public Category getById(int categoryId)
    {
        try
        {
            return (Category) getSession().get(Category.class, categoryId);
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public List<Category> findAll()
    {
        try
        {
            return getSession().createQuery("from Category").list();
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public boolean insert(Category category)
    {
        try
        {
            getHibernateTemplate().save(category);
            ValidatePhonePubSubClient.changeCategory();
            return true;
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public boolean update(Category category)
    {
        try
        {
            
                getHibernateTemplate().update(getHibernateTemplate().merge(category));
                ValidatePhonePubSubClient.changeCategory();
                return true;
           
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public boolean delete(Category category)
    {
        try
        {
            if (getById(category.getId()) == null)
            {
                return false;
            }
            else
            {
                getHibernateTemplate().delete(getHibernateTemplate().merge(category));
                ValidatePhonePubSubClient.changeCategory();
                return true;
            }
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }
}